﻿using System.Collections.Generic;
using UnityEngine;

namespace ZSW.Framework.Utils
{
    public static class ZSWFUtil_GameObject
    {
        public static void Reset(this Transform transform)
        {
            transform.localPosition = Vector3.zero;
            transform.localRotation = Quaternion.identity;
            transform.localScale = Vector3.one;
        }

        /// <summary>
        /// 清除 transform 下所有子对象
        /// </summary>
        /// <param name="parent"></param>
        public static void ClearAllChildrenObjs(this Transform parent, int startIndex = 0)
        {
            for (int i = parent.childCount - 1; i >= startIndex; i--)
            {
                Transform.Destroy(parent.GetChild(i).gameObject);
            }
        }

        /// <summary>
        /// 清除 transform 下所有子对象
        /// </summary>
        /// <param name="parent"></param>
        public static void HideAllChildrenObjs(this Transform parent, int startIndex = 0)
        {
            for (int i = parent.childCount - 1; i >= startIndex; i--)
            {
                parent.GetChild(i).gameObject.SetActive(false);
            }
        }

        /// <summary>
        /// 清除 transform 下除第一个子对象外的所有子对象
        /// 并隐藏第一个子对象
        /// </summary>
        /// <param name="parent"></param>
        public static void ClearAllCloneChildrenObjs(this Transform parent)
        {
            for (int i = parent.childCount - 1; i >= 1; i--)
            {
                Transform.Destroy(parent.GetChild(i).gameObject);
            }

            parent.GetChild(0).gameObject.SetActive(false);
        }

        /// <summary>
        /// 设置游戏对象数组可视
        /// </summary>
        /// <param name="gameObjects"></param>
        public static void SetActive(this GameObject[] gameObjects, bool state)
        {
            foreach (GameObject gameObject in gameObjects)
            {
                gameObject.SetActive(state);
            }
        }

        /// <summary>
        /// 切换游戏对象的可视
        /// </summary>
        /// <param name="gameObject"></param>
        public static void ToggleActive(this GameObject gameObject)
        {
            gameObject.SetActive(!gameObject.activeSelf);
        }

        /// <summary>
        /// 设置 transform 的游戏对象的可视
        /// </summary>
        /// <param name="t"></param>
        /// <param name="state"></param>
        public static void SetActive(this Transform t, bool state)
        {
            t.gameObject.SetActive(state);
        }

        /// <summary>
        /// 设置对象是否激活
        /// </summary>
        /// <param name="t"></param>
        /// <param name="state"></param>
        /// <typeparam name="T"></typeparam>
        public static void SetActive<T>(this T t, bool state) where T : Behaviour
        {
            t.gameObject.SetActive(state);
        }

        /// <summary>
        /// 设置 transform 下所有子对象的激活状态
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="state"></param>
        public static void SetChildrenActive(Transform parent, bool state)
        {
            for (int i = 0; i < parent.childCount; i++)
            {
                parent.GetChild(i).gameObject.SetActive(state);
            }
        }

        /// <summary>
        /// 实例化
        /// </summary>
        /// <param name="t"></param>
        /// <param name="parent"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T Instantiate<T>(T t, Transform parent = null) where T : Object
        {
            return GameObject.Instantiate(t, parent);
        }

        /// <summary>
        /// 获取Transform下所有包含隐藏的组件
        /// </summary>
        /// <param name="t"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T[] GetComponentsInChildrenWithHidden<T>(Transform t)
        {
            List<T> ts = new List<T>();
            if (t.childCount == 0)
            {
                return ts.ToArray();
            }

            foreach (Transform node in t)
            {
                T com = node.GetComponent<T>();
                if (com != null)
                {
                    ts.Add(com);
                }

                foreach (T childCom in ZSWFUtil_GameObject.GetComponentsInChildrenWithHidden<T>(node))
                {
                    ts.Add(childCom);
                }
            }

            return ts.ToArray();
        }

        /// <summary>
        /// 设置层级
        /// </summary>
        /// <param name="layerName"></param>
        public static void SetLayer(GameObject obj, string layerName, bool isRecursive = false)
        {
            obj.layer = LayerMask.NameToLayer(layerName);
            if (isRecursive)
            {
                foreach (Transform child in obj.transform)
                {
                    ZSWFUtil_GameObject.SetLayer(child.gameObject, layerName, isRecursive);
                }
            }
        }
    }
}